const { constants } = require('crypto');
var jwt = require('jsonwebtoken');
const secret = 'secretkey';
const jsonwebtoken = {
    //生成token
    getToken: function (val) {
        return new Promise((resolve, reject) => {
            const user = val;
            const token = jwt.sign(user, secret, { expiresIn: '1day' })
            resolve(token)
        })

    },
    //解析token
    decodeToken: function (token) {
        return new Promise((resolve, reject) => {
            if (!token) {
                reject({
                    error: 'token 是空的'
                })
            }
            const info = jwt.verify(token,secret, function (err, decoded) {
                if (!err) {
                    return decoded;
                }
                return { error: '过期了' }
            });
            resolve(info); 
        })
    }
}
module.exports = jsonwebtoken